package com.xyht.sca_s.student_manage_system.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.common.entity.SmsApplyDraft;
import com.xyht.sca_s.student_manage_system.modules.common.entity.req.SmsApplyDraftReq;
import com.xyht.sca_s.student_manage_system.modules.common.entity.resp.SmsApplyDraftResp;
import com.xyht.sca_s.student_manage_system.modules.common.mapper.SmsApplyDraftMapper;
import com.xyht.sca_s.student_manage_system.modules.common.service.SmsApplyDraftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 申请表草稿箱 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-06-20
 */
@Service
public class SmsApplyDraftServiceImpl extends ServiceImpl<SmsApplyDraftMapper, SmsApplyDraft> implements SmsApplyDraftService {
    @Autowired
    private SmsApplyDraftMapper smsApplyDraftMapper;


    @Override
    public ResponseResult addApplyDraft(SmsApplyDraftReq smsApplyDraftReq, String user_id) {
        if (isNullOrEmpty(smsApplyDraftReq.getDraftContent()) || isNullOrEmpty(smsApplyDraftReq.getAppType())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<SmsApplyDraft> list = smsApplyDraftMapper.selectList(new QueryWrapper<SmsApplyDraft>()
                .lambda()
                .eq(SmsApplyDraft::getAppType, smsApplyDraftReq.getAppType())
                .eq(SmsApplyDraft::getUserId, user_id));
        if (list == null || list.size() < 1) {
            SmsApplyDraft smsApplyDraft = new SmsApplyDraft();
            smsApplyDraft.setDraftContent(smsApplyDraftReq.getDraftContent());
            smsApplyDraft.setAppType(smsApplyDraftReq.getAppType());
            smsApplyDraft.setUserId(user_id);

            smsApplyDraftMapper.insert(smsApplyDraft);
        } else {
            SmsApplyDraft updateDraft = new SmsApplyDraft();
            updateDraft.setDraftContent(smsApplyDraftReq.getDraftContent());
            smsApplyDraftMapper.update(updateDraft, new QueryWrapper<SmsApplyDraft>()
                    .lambda()
                    .eq(SmsApplyDraft::getAppType, smsApplyDraftReq.getAppType())
                    .eq(SmsApplyDraft::getUserId, user_id));
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult getOwnApplyDraft(String appType, String user_id) {
        List<SmsApplyDraft> list = smsApplyDraftMapper.selectList(new QueryWrapper<SmsApplyDraft>()
                .lambda()
                .eq(SmsApplyDraft::getAppType, appType)
                .eq(SmsApplyDraft::getUserId, user_id)
                .orderByDesc(SmsApplyDraft::getCreateTime));
        SmsApplyDraftResp smsApplyDraftResp = new SmsApplyDraftResp();
        if (list != null && list.size() > 0) {
            SmsApplyDraft smsApplyDraft = list.get(0);
            BeanUtils.copyProperties(smsApplyDraft,smsApplyDraftResp);
        }
        if (smsApplyDraftResp.getId() == null){
            smsApplyDraftResp = null;
        }
        return CommonResult.success(smsApplyDraftResp);
    }

    @Override
    public ResponseResult deleteApplyDraft(SmsApplyDraftReq smsApplyDraftReq) {
        if (isNullOrEmpty(smsApplyDraftReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsApplyDraft smsApplyDraft = smsApplyDraftMapper.selectById(smsApplyDraftReq.getId());
        if (smsApplyDraft == null){
            return CommonResult.failed(CommonCodeEnum.DRAFT_NOT_EXIST);
        }
        smsApplyDraftMapper.deleteById(smsApplyDraftReq.getId());
        return CommonResult.success();
    }
}
